package org.example.struct.array;

import java.util.*;

/**
 * @author liyishan
 * @date 2023/12/26 10:19
 * @apiNote
 */

public class Test01636FrequencySort {

    public static int[] frequencySort(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        List<Integer> list = new ArrayList<>();
        for (int num:nums){
            list.add(num);
        }
        Collections.sort(list,(a,b) ->{
            int cnt1 = map.get(a), cnt2 = map.get(b);
            return cnt1 != cnt2 ? cnt1 - cnt2 : b - a;
        });
        int length = nums.length;
        for(int i = 0; i < length; i++){
            nums[i] = list.get(i);
        }
        return nums;
    }
}
